<?php

declare(strict_types=1);

namespace app\admin\controller;

use think\facade\View; //模板引擎
use app\common\Base;
use think\facade\Request;
use app\validate\DbdVal; //验证
use app\model\Dbd as DbdMod; //模块
use think\exception\ValidateException;
use tool\SqlBackup;
use tool\SqlRestore;

/**
 * @name 数据库设计
 */
class Dbd extends Base
{
    /**
     * @name 数据列表
     */
    public function dbdlist()
    {
        $list = DbdMod::Dbdlist();
        $BackupsFile = read_all(root_path() . '/public/backdir');
        $arr = [];
        foreach ($BackupsFile as $k => $v) {
            $arr[] = $v['path'];
        }
        foreach ($list as $k => $v) {
            if (in_array($v['Name'] . '#0.sql', $arr)) {
                $v['is_backups'] = '1';
            } else {
                $v['is_backups'] = '0';
            }
            $data[] = $v;
        }
        if (request()->isGet()) {
            $page = Request::param('page');
            if (isset($page)) {
                return getBack(0, 'ok', $data, count($data));
            }
        }
        return View::fetch();
    }

    /**
     * @name 数据表添加
     */
    public function dbdadd()
    {
        if (request()->isPost()) {
            $param = Request::param();
            try { //进入验证
                validate(DbdVal::class)->check($param);
            } catch (ValidateException $e) {
                return getBack(1, $e->getError());
            }
            $res = DbdMod::Dbdadd($param);
            if ($res == 'success') {
                return getBack(0);
            } else {
                return getBack(400, $res);
            }
        }
        return View::fetch();
    }

    /**
     * @name 数据表删除
     */
    public function dbddel()
    {
        if (request()->isGet()) {
            $name = Request::param('name');
            if (isset($name) && $name != '') {
                $res = DbdMod::Dbddel($name);
                if ($res == 'success') {
                    return getBack(0);
                } else {
                    return getBack(2, $res);
                }
            } else {
                return getBack(1, '参数错误!');
            }
        }
    }

    /**
     * @name 数据表备份
     */
    public function dbdbackups()
    {
        if (request()->isPost()) {
            $name = Request::param('backdir');
            if (!is_dir('backdir')) {
                mkdir('backdir', 0777, true);
            }
            $backup = new SqlBackup;
            $res = $backup->SynBackups($name);
            return json($res);
        }
    }

    /**
     * @name 数据表还原
     */
    public function restore()
    {
        if (request()->isPost()) {
            $backup = new SqlRestore;
            $res = $backup->SynRestore();
            return json($res);
        }
    }

    /**
     * @name 数据表下载
     */
    public function download()
    {
        if (request()->isGet()) {
            $name = Request::param('name').'#0.sql';
            return download('backdir/' . $name, $name);
        }
    }

    /**
     * @name 数据字段列表
     */
    public function fieldlist()
    {
        if (request()->isGet()) {
            $name = Request::param('name');
            // dump(DbdMod::Dbdfields($name));
            View::assign('table', $name);
            $page = Request::param('table');
            if (isset($page)) {
                $list = DbdMod::Dbdfields($page);
                return getBack(0, 'ok', $list, count($list));
            }
        }
        return View::fetch();
    }

    /**
     * @name 数据字段添加
     */
    public function fieldadd()
    {
        if (request()->isPost()) {
            $param = Request::param();
            $res = DbdMod::fieldadd($param);
            if ($res == "success") {
                return getBack(0);
            } else {
                return getBack(3, $res);
            }
        }
        return View::fetch();
    }

    /**
     * @name 数据字段删除
     */
    public function fielddel()
    {
        if (request()->isGet()) {
            $param = Request::param();
            $res = DbdMod::fielddel($param);
            if ($res == "success") {
                return getBack(0);
            } else {
                return getBack(3, $res);
            }
        }
        return View::fetch();
    }
}
